﻿
namespace ZDS.Test

open System
open System.Collections.Generic

open NUnit.Framework
open FsUnit

open ZDS

[<TestFixture>]
type TestArrayList() =

    [<Test>]
    member this.TestGet() =
        let a1 = new ZArrayList<int>(2)
        a1.Size |> should equal 0
        
        (fun() -> a1.[0] |>ignore) |> TestUtility.getErrorCode |> should equal ErrorCode.OutofRange

    [<Test>]
    member this.TestSet() =
        let x = new ZArrayList<string>(2)
        
        let a = [|"cheka";"stasi"|]
        a |> Seq.iter (x.Insert 0)
        x |> should equal (Array.rev a)

        x.[0] <- "kgb"
        x |> should equal ["kgb";"cheka"]

        (fun() -> x.[2] <- "cia")
        |> TestUtility.getErrorCode
        |> should equal ErrorCode.OutofRange

    [<Test>]
    member this.TestAppend() =
        let a = new ZArrayList<int>(2)

        for index = 1 to a.Capacity do
            a.Append index

        a |> should equal [1;2;]

        (fun() -> a.Append 100) |> TestUtility.getErrorCode |> should equal ErrorCode.Full

    [<Test>]
    member this.TestIterate() =
        let a = new ZArrayList<string>(3)
        a.Append("stasi")
        a.Append("kgb")

        a |> should equal ["stasi";"kgb"]

        // ------------ compatible with all methods in Seq module
        a 
        |> Seq.map (fun item -> item.Length)
        |> should equal [5;3]

    [<Test>]
    member this.TestInsert() =
        let a = new ZArrayList<int>(3)

        (fun() -> a.Insert 5 100 ) 
        |> TestUtility.getErrorCode
        |> should equal ErrorCode.OutofRange

        for index = 1 to a.Capacity do
            a.Insert 0 index
        a |> should equal [3;2;1]

        (fun() -> a.Insert 5 100 ) 
        |> TestUtility.getErrorCode
        |> should equal ErrorCode.Full

    [<Test>]
    member this.TestRemove() =
        // ------------------------------ test 1
        let a = new ZArrayList<int>(4)

        for index = 1 to 3 do
            a.Append index

        a.Remove 1 |> should equal 2
        a |> should equal [1;3]

        a.Remove 0 |> should equal 1
        a.Remove 0 |> should equal 3

        (fun() -> a.Remove 100 |> ignore) 
        |> TestUtility.getErrorCode
        |> should equal ErrorCode.Empty

        // ------------------------------ test 2
        let b = new ZArrayList<int>(3)

        for index = 1 to 2 do
            b.Append <| index*index

        let numbers = seq {
            for index = 1 to b.Size do
                yield b.Remove (b.Size - 1)
        }
        numbers |> should equal [4;1]
        b.Size |> should equal 0

